package com.library.service.admin;

import com.library.dao.BookCopyDao;
import com.library.dao.BookDao;
import com.library.dao.BorrowrecDao;
import com.library.dao.StudentDao;
import com.library.entity.Book;
import com.library.entity.po.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class BookService {

    private final BorrowrecDao borrowrecDao = new BorrowrecDao();
    private final BookDao bookDao = new BookDao();
    private final StudentDao studentDao = new StudentDao();
    private final BookCopyDao bookCopyDao = new BookCopyDao();

    /**
     * 获取图书排行榜
     *
     * @return
     */
    public Set<BookRankPO> getBookRank() {
        // 1.获取书籍副本借阅记录统计
        Map<String, Integer> bookCopyRecs = borrowrecDao.countBookCopyRecs();

        Map<String, Integer> bookRankMap = new HashMap<>(); // 统计书籍借阅次数
        // 2.获取书籍副本对应名称，并统计数量
        for (Map.Entry<String, Integer> entry : bookCopyRecs.entrySet()) {
            String barCode = entry.getKey();
            String bname = bookDao.getNameByBarCode(barCode);
            
            // 处理书名为null的情况
            if (bname == null || bname.trim().isEmpty()) {
                bname = "未知书籍";
            }
            
            if (!bookRankMap.containsKey(bname)) {
                bookRankMap.put(bname, entry.getValue());
            } else {
                bookRankMap.put(bname, bookRankMap.get(bname) + entry.getValue());
            }
        }

        // 3.将结果转换为BookRankPO，存入TreeSet
        Set<BookRankPO> bookRanks = new TreeSet<>();
        for (Map.Entry<String, Integer> entry : bookRankMap.entrySet()) {
            BookRankPO bookRankPO = new BookRankPO(entry.getKey(), entry.getValue());
            bookRanks.add(bookRankPO);
        }
        return bookRanks;
    }

    /**
     * 获取学生借阅排行榜
     *
     * @return
     */
    public Set<StudentRankPO> getStudentRank() {
        // 获取学生借阅记录统计
        Map<String, Integer> studentRecs = borrowrecDao.countStudentRecs();

        Set<StudentRankPO> studentRank = new TreeSet<>();

        for (Map.Entry<String, Integer> entry : studentRecs.entrySet()) {
            String sno = entry.getKey();
            String username = studentDao.getNameBySno(sno);
            studentRank.add(new StudentRankPO(sno, username, entry.getValue()));
        }
        return studentRank;
    }

    /**
     * 分页查询图书信息
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @param request
     * @return
     */
    public PagePO<BookPO> pageBook(int pageNum, int pageSize, String keyword, HttpServletRequest request) {
        // 1.分页查询书籍信息
        List<Book> bookList = null;
        // 2.获取总记录数
        Long totalCount = null;
        if (keyword == null || keyword.trim().isEmpty()) {
            bookList = bookDao.listBookByPage(pageNum, pageSize);
            totalCount = bookDao.getTotalCount();
        } else {
            bookList = bookDao.pageBooks(keyword, pageNum, pageSize);
            totalCount = bookDao.getTotalCountByKeyword(keyword);
            request.setAttribute("keyword", keyword);
        }

        List<BookPO> bookPOList = new ArrayList<>();
        for (Book book : bookList) {
            // 3.查询书籍副本数量
            Long count = bookCopyDao.countByISBN(book.getISBN());
            // 4.查询书籍副本借出数量
            Long borrowCount = bookCopyDao.countBorrowByISBN(book.getISBN());
            bookPOList.add(new BookPO(book, count, borrowCount));
        }

        return new PagePO<>(bookPOList, pageNum, pageSize, totalCount, (int) Math.ceil((double) totalCount / pageSize));

    }


}
